public class Weapon extends UnityEngine.MonoBehaviour{
	
	private var wName: String;
	private var magCap:int;
	//private static var weight:int; //I dont think we need this....
	private var pow:int; //This is how much is subtracted from enemy or player health
	private var oneHanded:boolean;
	private var fireRate:double;
	private var ammo:int; 		
	private var reloadTime: double;
	private var ammoPool:int;
	private var range:int;
	
	
	private static var ftime:double = 0.0; //DO NOT CHANGE THIS OR OVERWRITE THIS
	private static var rtime:double = 0.0; //DO NOT CHANGE THIS OR OVERWRITE THIS TOO
	private static var clipEmpty = false; //DO NOT CHANGE THIS OR OVERWRITE THIS A SECOND TIME
	
	private static var player:GameObject;
	
	public function Weapon(name: String, magCapacity: int, power: int, fRate: double, reloadTime: double, handed: boolean, range: int, pool:int){
		this.wName = name;
		this.magCap = magCapacity;
		this.pow = power;
		ammo = magCapacity;
		this.fireRate = fRate;
		this.reloadTime = reloadTime;
		this.oneHanded = handed;
		this.range = range;
		this.ammoPool = pool;
	}
	public function fire(){
		//print("fire");
		if(ammo>0&&ammoPool>0&&!clipEmpty){
			if(Time.time-ftime>fireRate){
				ftime=Time.time;
				doFire();
				ammo--;
				print(ammo);
			}
		}
		else if(ammoPool<=0&&!clipEmpty){
			print("Clip Empty");
			clipEmpty=true;
		}
	}
	
	public function reload(): IEnumerator {
		if(!(ammo==magCap)){
			if(Time.time-rtime>reloadTime){
				print("RELOADING");
				rtime = Time.time;
				yield WaitForSeconds(reloadTime);
				print("RELOADED");
				rtime = Time.time;
				ammoPool = ammoPool-(magCap-ammo);
				ammo = magCap;	////not magCap though, the number from the player script
			}
		}
	}
	//THIS NOT BE USED ANYMORE, SAAAAAN!!!!!
	public function altFire() {
		if(ammo>=0&&ammoPool>=0){
			if(Time.time-ftime>fireRate){
				//print("BANG");
				player = GameObject.FindGameObjectWithTag("Player");
				//print(player);
				var fwd = player.transform.TransformDirection (Vector3.forward);
				if (Physics.Raycast(player.transform.position, fwd, 10)  ) { //player.transform.position
	            	print(ammo+" Insert ACCURATE hitmarkers here");
	       		}
	       		else{
	       			print(ammo);
	       		}
				
				ftime=Time.time;
				ammo--;
			}
		}
	}
	public function doFire(){
	    var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0)); 
	    var hit : RaycastHit;
	    if (Physics.Raycast (ray, hit, range)) {
	        print("THIS IS A HIT");
	        print(hit.transform.gameObject);
	        if(hit.transform.gameObject.tag == "Enemy"){
		        var enemy:Enemy = hit.transform.gameObject.GetComponent("Enemy");
		        if(enemy.health<=0){
		        	print("Its dead.");
		        }   
		        else{
		       		print(enemy.health); 
		        	enemy.healthUpdate(pow*-1);
		        }
	        }
	        
	    }
	}
	public function getPow(): int{
		return pow;
	}
	public function getAmmo(): int{
		return ammo;
	}
	function toString(): String{
		return wName;
	}
}